/*****************************************************************************
Copyright 2006 Kervin L. Pierre
    EMAIL: otlkcon@adevsoft.com

This file is part of Otlkcon.

Otlkcon is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

Otlkcon is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Lesser GNU General Public License for more details.

You should have received a copy of the Lesser GNU General Public License
along with Otlkcon; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*******************************************************************************/
#include <stdafx.h>

#include <otlkcon_config.h>
#include <otlkcon_results.h>
#include <otlkcon_registry.h>

char *otlkcon_registryGetStr(LPALLOCATEBUFFER lpAllocateBuffer, HKEY hk, char *key )
{ 
    CHAR szBuf[OTLKCON_REG_VALUE_MAX_LEN+1] = {0};
	DWORD szBufSize = OTLKCON_REG_VALUE_MAX_LEN;
	DWORD readRetVal;
    char *result;
    size_t memSize;

	readRetVal = RegQueryValueEx(hk, key, NULL, NULL, (LPBYTE)szBuf, &szBufSize);
	if( readRetVal != ERROR_SUCCESS )
	{
        //OTLKCON_LOG_PRINT_LAST_ERROR();
        return 0; 
	}
    
    memSize = strlen(szBuf)+1;
    if( memSize >= ULONG_MAX )
        return 0;

    if( lpAllocateBuffer )
    {
	    lpAllocateBuffer( (ULONG)memSize, (LPVOID *)&result );
    }
    else
    {
        MAPIAllocateBuffer( (ULONG)memSize, (LPVOID *)&result );
    }

    strcpy(result, szBuf);

    return result;
}

int otlkcon_registryGetInt( HKEY hk, char *key )
{
    DWORD szBuf     = 0;
	DWORD szBufSize = 4;
	DWORD readRetVal;

    readRetVal 
		= RegQueryValueEx( hk, key, NULL, NULL, (LPBYTE)&szBuf, &szBufSize);
	if( readRetVal != ERROR_SUCCESS )
	{
        OTLKCON_LOG_PRINT_LAST_ERROR();
        return 0; 
	}

	return (int)szBuf;
}

OTLKCON_RESULT_TYPE otlkcon_registrySetStr( HKEY reg, char *key, char *value )
{
    return 0;
}

OTLKCON_RESULT_TYPE otlkcon_registrySetInt( HKEY reg, char *key, int value )
{
    return 0;
}

HKEY otlkcon_registryInit(LPALLOCATEBUFFER lpAllocateBuffer, DWORD access, char *subKey)
{
    HKEY hk;  
    char *key;
    size_t memSize;

    if(subKey)
    {    
        memSize = strlen(OTLKCON_REG_KEY_A)+strlen(subKey)+3;
        if( memSize >= ULONG_MAX)
            return 0;

        if( lpAllocateBuffer )
        {
            lpAllocateBuffer((DWORD)memSize, (LPVOID *)&key);
        }
        else
        {
            MAPIAllocateBuffer( (ULONG)memSize, (LPVOID *)&key );
        }

        sprintf(key, "%s\\%s", OTLKCON_REG_KEY, subKey);
    }
    else
    {
        key = OTLKCON_REG_KEY_A;
    }

    if(access==0)
        access=KEY_QUERY_VALUE;

	if( RegOpenKeyEx(HKEY_CURRENT_USER, key,
		0, access, &hk) != ERROR_SUCCESS )
	{
        OTLKCON_LOG_PRINT_LAST_ERROR();
        return 0; 
	}

    return hk;
}

void otlkcon_registryDispose(HKEY hk)
{
    RegCloseKey(hk);
}

bool otlkcon_registryGetStrW(
	HKEY reg,
	wchar_t *key,
	wchar_t *lpszBuffer,
	size_t cchBufferLength)
{
	bool bResult = false;

	if(NULL != lpszBuffer && 0 < cchBufferLength)
	{
		*lpszBuffer = L'\0';
		
		DWORD szBufSize = (DWORD)(cchBufferLength * sizeof(wchar_t));
		DWORD readRetVal;
		readRetVal = RegQueryValueExW(reg, key, NULL, NULL, (LPBYTE)lpszBuffer, &szBufSize);
		if( readRetVal == ERROR_SUCCESS || readRetVal == ERROR_MORE_DATA)
		{
			if(readRetVal == ERROR_MORE_DATA)
			{
				lpszBuffer[cchBufferLength-1] = L'\0';
			} // if
			bResult = true;
		}
	} // if
	return bResult;
}
